<?php
/**
 *
 * @file Row.php
 *
 * @todo brief.
 *
 * @todo details here
 *
 * http://code.google.com/p/rpd/wiki/Row.php
 *
 * New BSD License
 * \n
 * Copyright (c) 2010 Rpd-Team
 *
 * @namespace  rpd
 * @since      0.0.1
 * @version    0.0.1
 */

// namespace ;

class Model_Row
{
	private $_data;
	private $_model;

	function __construct(Db_Statement $statement, Model $model)
	{
		$this->_data = $statement->current();
		$this->_model = $model;
	}

	function __set($var, $value)
	{
		if (isset($this->_data[$var]))
		{
			$query = "UPDATE " . $this->_model->getTable()
			       . " SET `$var` = '%s'"
			       . " WHERE 1 ";

			foreach ($this->_model->getJoinedTables() as $table)
			{
				$tablename = strtok($table->getTable(), " ");
				$query .= " && $tablename.id$tablename = "
				       .  $this->_data["id".$tablename];
			}

			$this->_model->query($query, $value);

			$this->_data[$var] = $value;
		}
	}

	function __get($var)
	{
		var_dump($var);
		if (isset($this->_data[$var]))
		{
			return $this->_data[$var];
		}
		else if ($this->_model->isJoinedTable($var))
		{
			return $this;
		}
	}
}

?>